package com.xthh._function._socket;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class HH_HttpUtil {
    protected static final String TAG = "XTHH_HttpUtil";
    protected static Bitmap bitmapFormUrl;
    // 获取HTTP数据
    public static String GetHttpInfo(String url) {
        if (url == null || url.length() <= 0)
            return "";


        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try {
            response = okHttpClient.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (response != null && response.isSuccessful()) {
            String info;
            try {
                info = response.body().string();

                return info;
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {

        }

        return "";
    }

    public static String HttpPostSync(String url, String json) {
        try {
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
            Request request = new Request.Builder().url(url).post(requestBody).build();

            Response response = client.newCall(request).execute();
            String responseData = response.body().string();
            return responseData;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Bitmap getBitmap_1(String path) {
        URL url = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        conn.setConnectTimeout(5000);
        try {
            conn.setRequestMethod("GET");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        try {
            if(conn. getResponseCode() == 200){
                InputStream inStream = conn. getInputStream();
                Bitmap bitmap = BitmapFactory. decodeStream(inStream) ;
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static Bitmap getBitmap_2(String photoUrl) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;

                try {
                    imageurl = new URL(photoUrl);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    if (is == null){
                        throw new RuntimeException("stream is null");
                    }else{
                        try {
                            byte[] data=readStream(is);
                            if(data!=null){
                                bitmapFormUrl = BitmapFactory.decodeByteArray(data, 0, data.length);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        is.close();
                    }




                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return bitmapFormUrl;
    }

    /*
     * 得到图片字节流 数组大小
     * */
    public static byte[] readStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1){
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inStream.close();
        return outStream.toByteArray();
    }
}
